package com.example.web_back.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.web_back.mapper.UserMapper;
import com.example.web_back.entity.Result;
import com.example.web_back.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * User控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;


    /**
     * 登陆方法
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        //在数据库中查询与传来的用户名和密码系统的数据
        System.out.println(user.getUsername()+user.getUserpw());
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername,user.getUsername()).eq(User::getUserpw,user.getUserpw()));
        if (res==null){
            return Result.error("-1","用户名或密码错误");
        }else {
            return Result.success(res);
        }
    }

    /**
     * 注册方法
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result Register(@RequestBody User user){
        //在数据库中查询与传来的用户名和密码系统的数据
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername,user.getUsername()));
        if (res!=null){
            return Result.error("-1","用户名重复");
        }else {
            userMapper.insert(user);
            return Result.success(res);
        }
    }

    @PostMapping("/getAllUsers")
    public List<User> getAllUsers() {
        List<User> userList = userMapper.selectList(null);
        System.out.println(userList.get(0));
        return userList;
    }

    /**
     * 新增数据操作
     *
     * @param user
     * @return
     */
    @Transactional
    @PostMapping
    //@RequestBody注解，将传入的json数据转换为对象
    public Result saveUser(@RequestBody User user) {
        if (user.getUserid() != null) {
            userMapper.updateById(user);
        } else {
            userMapper.insert(user);
        }
        return Result.success();
    }

    /**
     * 根据ID删除数据
     * @param
     * @return
     */
    @Transactional
    @DeleteMapping("/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        userMapper.deleteById(userId);
        return Result.success();
    }

    /**
     * 分页查询User表
     * 根据前端传来的搜索值进行模糊擦查询
     *
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    @GetMapping
    public Result findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(defaultValue = "") String search) {
        // Wrappers.<User>lambdaQuery().like(User::getNickname,search) 模糊查询，根据前端传来的search
        Page<User> page = userMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<User>lambdaQuery().like(User::getUsername, search));
        return Result.success(page);
    }

}